function showImg(){
    layer.photos({
        photos: '.layui-table'
        ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
    });
}
layui.use(['form','layer','table','laytpl','laydate'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table,
        laydate = layui.laydate;


    //日期范围
    laydate.render({
        elem: '#dataselect'
        ,range: true
    });

    //保险信息列表
    var tableIns = table.render({
        elem: '#insuranceList',
        url : '../insurance/getInsuranceList',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        loading:true,
        id : "insuranceListTable",
        cols : [[
            {field: 'name', title: '保险项', align:"center"},
            {field: 'userName', title: '客户名称', align:"center"},
            {field: 'phone', title: '电话', align:'center'},
            {field: 'plateNum', title: '身份证正面', align:'center',templet: function(d) {
                    return "<img src='"+d.cardZheng+"' onclick='showImg()' class='insImg' width='50px' height='50px' />"
                }},
            {field: 'invite', title: '身份证反面', align:'center',templet: function(d) {
                    return "<img src='"+d.cardFan+"'  class='insImg' onclick='showImg()'  width='50px' height='50px' />"
                }},
            {field: 'plateNum', title: '行驶证正面', align:'center',templet: function(d) {
                    return "<img src='"+d.driverZheng+"'  onclick='showImg()' class='insImg'  width='50px' height='50px' />"
                }},
            {field: 'invite', title: '行驶证反面', align:'center',templet: function(d) {
                    return "<img src='"+d.driverFan+"' onclick='showImg()'  class='insImg'  width='50px' height='50px' />"
                }},
            {field: 'createTime', title: '上报时间', align:'center'},
            {field: 'state', title: '反馈状态', align:'center', templet: function(d){
                    if(d.state=="1"){
                        return "未反馈"
                    }else{
                        return "已反馈";
                    }
                }
             },
            {title: '操作', templet: '#insuranceListBar', fixed: "right", align: "center"}
        ]]
    });
    //日期范围
    laydate.render({
        elem: '#dataselect'
        , range: true
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        tableIns.reload({
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: { //设定异步数据接口的额外参数，任意设
                userName:$(".name").val(),
                phone:$(".phone").val(),
                state:$(".state").val(),
                date:$("#dataselect").val()
            }
        })
    });

    function editType(data) {
        var index = layui.layer.open({
            title: "操作分类",
            type: 2,
            content: "../page/toEditInsurance?id="+data.id,
            success: function () {
                var body = layui.layer.getChildFrame('body', index);
                if (data) {
                    body.find(".id").val(data.id);
                    form.render();
                }
                setTimeout(function () {
                    layui.layer.tips('点击此处返回保险信息管理列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize", function () {
            layui.layer.full(index);
        })
    }

    $(".add_btn").click(function () {
        editType();
    });

    function deleteById(data) {
        layer.confirm('确定删除选中的保险信息!', {icon: 3, title: '提示信息'}, function (index) {
            console.log(data.id);
            $.ajax({
                url: '../insurance/deleteById',
                data: {id:data.id},
                method: 'post',
                dataType: 'json',
                success: function (res) {
                    console.log(res)
                    if (res.code == 200) {
                        layer.msg("删除成功");
                        tableIns.reload();
                        layer.close(index);
                    }else {
                        layer.msg("删除失败");
                        layer.close(index);
                    }

                },
                error: function () {
                    layer.msg("系统繁忙,请稍后再试!");
                    layer.close(index);
                }
            });
        });
    }

//列表操作
    table.on('tool(insuranceList)', function (obj) {
        var layEvent = obj.event,
            //当前行数据
            data = obj.data;

        if (layEvent === 'edit') {
            editType(data);
        }
        if (layEvent === 'delete') {
            deleteById(data);
        }
    });
});